import React, { Component } from 'react'

class Main extends Component {

  // 此处的数据的它只执行1次
  state = {
    currentIndex: 0,
    // currentKey: Object.keys(this.props.data).shift()
    currentKeys: ['', '']
  }

  // 修改当前的左侧点击菜单
  setCurrentIndex = (currentIndex, currentKey) => {
    this.setState(state => {
      let currentKeys = state.currentKeys
      currentKeys[this.props.navTabCurrentIndex] = currentKey
      return {
        currentIndex,
        currentKeys
      }
    })
  }

  // 执行N次
  render() {
    let { currentIndex, currentKeys } = this.state
    let { data, navTabCurrentIndex } = this.props

    let mainData = currentKeys[navTabCurrentIndex] === '' ? data[Object.keys(data)[0]] : data[currentKeys[navTabCurrentIndex]]

    return (
      <div className='category-main-container'>
        <div className='slider'>
          <ul>
            {
              Object.keys(data).map((item, index) => (
                <li onClick={() => this.setCurrentIndex(index, item)} key={index} class={currentIndex === index ? 'active' : ''}>
                  <span>{item}</span>
                </li>
              ))
            }
          </ul>
        </div>
        <div className='main'>
          <ul>
            {
              mainData && mainData.map((item, index) => (
                <li key={index}>
                  {item}
                </li>
              ))
            }
          </ul>
        </div>
      </div>
    )
  }
}

export default Main
